package cn.campus;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Order {
    /*-- 步骤 2 属性提炼 & 步骤 3 自动编号 --*/
    private static long nextId = 1000L; // 类变量：全局自增
    private final long orderId; // 实例变量：唯一号码
    private final String goods;
    private final double amount;
    private final String destination;
    private final LocalDateTime createTime; // 新增：下单时间属性

    /*-- 步骤 3 构造方法 --*/
    public Order(String goods, double amount, String destination) {
        this.orderId = nextId++;
        this.goods = goods;
        this.amount = amount;
        this.destination = destination;
        this.createTime = LocalDateTime.now(); // 初始化下单时间为当前时间
    }

    /*-- 新增：获取下一个即将分配的ID --*/
    public static long getNextId() {
        return nextId;
    }

    /*-- 步骤 4 重写 toString --*/
    @Override
    public String toString() {
        // 格式化时间输出（可选，让时间显示更友好）
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return "订单号: " + orderId + "\n" +
                "商品: " + goods + "\n" +
                "金额: " + amount + "\n" +
                "目的地: " + destination + "\n" +
                "下单时间: " + createTime.format(formatter) + "\n"; // 新增：打印下单时间
    }
}